﻿using System;
using System.Text.RegularExpressions;

namespace QDT.Payment.AllInPay.Api
{
    public class SingleRtColPayRequest : Request
    {
        /// <summary>报文XML文档TRANS对象</summary>
        //protected XmlElement m_elTrans;

        /// <summary>
        /// 批量代付请求
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="reqSn">交易流水号（40位）</param>
        /// <param name="level">处理级别，0-9</param>
        /// <param name="businessCode">业务代码</param>
        /// <param name="merchantId">商户编号</param>
        /// <param name="term_code">终端编号</param>
        /// <param name="track2">磁道2</param>
        /// <param name="track3">磁道3</param>
        /// <param name="pinCode">pin码</param>
        /// <param name="pinFormat">PIN格式</param>
        /// <param name="e_user_code">用户编号</param>
        /// <param name="validDate">有效期</param>
        /// <param name="cvv2">信用卡CVV2</param>
        /// <param name="bankCode">银行代码</param>
        /// <param name="accoutType">账号类型</param>
        /// <param name="accountNo">账号</param>
        /// <param name="accountName">账号名称</param>
        /// <param name="accountProp">账号属性</param>
        /// <param name="amount">金额</param>
        /// <param name="currencyType">货币类型</param>
        /// <param name="idType">开户证件类型</param>
        /// <param name="ID">证件号</param>
        /// <param name="settAcct">本交易结算户</param>
        /// <param name="tel">手机号码</param>
        /// <param name="custUserId">自定义用户号</param>
        /// <param name="remark">备注</param>
        public SingleRtColPayRequest(string userName, string password, string reqSn, int level, string businessCode, string merchantId, string term_code,
                                     string track2, string track3, string pinCode, PinMt pinFormat,
                                     string e_user_code, string validDate, string cvv2, string bankCode,
                                     AccountType accoutType, string accountNo, string accountName, AccountProp accountProp, long amount,
                                     Currency currencyType, IDType idType, string ID, string settAcct, string tel, string custUserId, string remark)
            : base("TRANS", InternalConstants.SignReleaseTrxCode, "03", 2, userName, password, reqSn)
        {
            m_elInfo.AppendChild(CreateValueElement("LEVEL", level.ToString()));
            if (businessCode == null || !Regex.IsMatch(businessCode, "^.{6}$"))
                throw new FormatException("业务代码格式错误，应为：C(6)");
            m_elBody.AppendChild(CreateValueElement("BUSINESS_CODE", businessCode));
            if (merchantId == null)
                throw new FormatException("银联网络商户ID格式错误");
            m_elBody.AppendChild(CreateValueElement("MERCHANT_ID", merchantId));
            m_elBody.AppendChild(m_package.CreateElement("SUBMIT_TIME"));
            m_elBody.AppendChild(CreateValueElement("TERM_CODE", term_code));

            if (accountNo == null || !Regex.IsMatch(accountNo, "^[0-9]{1,32}$"))
                throw new FormatException("银行账号格式错误，应为：N(1,32)");
            m_elBody.AppendChild(m_package.CreateElement("ACCOUNT_NO", accountNo));

            if (accountName == null || !Regex.IsMatch(accountName, "^.{1,60}$"))
                throw new FormatException("银行账户名称格式错误，应为：C(1,60)");
            m_elBody.AppendChild(m_package.CreateElement("ACCOUNT_NAME", accountName));

        }
    }
}
